package de.zainodis.triplog.ui;

import android.app.Activity;
import android.os.Bundle;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import de.zainodis.triplog.R;

/**
 * Created by fzarrai on 10/6/2014.
 */
public class GoogleMapsActivity extends Activity {

    private static final String BUNDLE_LATLNG = "LatLon";
    private static final String BUNDLE_MARKER_TITLE = "MapMarkerTitle";

    // Might be null if Google Play services APK is not available.
    private GoogleMap map;
    private MarkerOptions markerOptions;
    //Cached so we can restore it in case of a config change event
    private LatLng latLng = null;
    private String markerTitle = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            //Restore previous instance
            latLng = savedInstanceState.getParcelable(BUNDLE_LATLNG);
            markerTitle = savedInstanceState.getString(BUNDLE_MARKER_TITLE);
        }
    }

    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #map} is not null.
     * <p/>
     * If it isn't installed {@link com.google.android.gms.maps.SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), {@link #onCreate(android.os.Bundle)} may not be called again so we should call this
     * method in {@link #onResume()} to guarantee that it will be called.
     */
    protected void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (map == null) {
            map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (map != null) {
                setUpMap();
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p/>
     * This should only be called once and when we are sure that {@link #map} is not null.
     */
    protected void setUpMap() {
        setMarker(latLng, markerTitle);
    }

    public LatLng getLatLng() {
        return latLng;
    }

    public void setLatLng(LatLng latLng) {
        this.latLng = latLng;
    }

    /**
     * Sets a marker on the map for the given coordinates. All arguments will be checked for null, so you can safely pass in null as well,
     * in which case of course, no marker will be set.
     *
     * @param position coordinates of the location.
     * @param title    the title of the marker.
     * @return true if a marker has been set; false otherwise.
     */
    protected boolean setMarker(LatLng position, String title) {
        // Cleanup the map
        map.clear();
        if (position == null || title == null || title.isEmpty()) {
            return false;
        }
        // Display the address on the map
        markerOptions = new MarkerOptions();
        markerOptions.position(position);
        markerOptions.title(title);

        map.addMarker(markerOptions);

        return true;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //Save lat/lon so we can restore it upon config change sa well as the weather
        if (latLng != null) {
            outState.putParcelable(BUNDLE_LATLNG, latLng);
            if (markerTitle != null && !markerTitle.isEmpty()) {
                outState.putString(BUNDLE_MARKER_TITLE, markerTitle);
            }

        }
    }

    public GoogleMap getMap() {
        return map;
    }
}
